import { data } from 'autoprefixer';
<template>
  <div>
    <div class="search">
      <input type="text"
             class="search-input"
             placeholder="请输入城市或拼音"
             v-model="keyword">
    </div>
    <div class="search-content"
         v-show="keyword"
         ref="searchContent">
      <ul>
        <li class="search-item border-bottom"
            v-for="item of list"
            :key="item.id"
            @click="handleCityClick(item.name)">{{item.name}}
        </li>
        <li v-show="hasData"
            class="search-item border-bottom">未搜索到相关城市</li>
      </ul>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapMutations } from 'vuex'
export default {
  name: 'CitySearch',
  props: {
    cities: Object
  },
  data () {
    return {
      list: [],
      keyword: '',
      timer: null
    }
  },
  methods: {
    handleCityClick (city) {
      // this.$store.dispatch('cityChange', city)
      this.cityChange(city)
      this.$router.push('/')
    },
    ...mapMutations(['cityChange'])
  },
  computed: {
    hasData () {
      return !this.list.length
    }
  },
  watch: {
    keyword () {
      if (!this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        let result = []
        for (let i in this.cities) {
          this.cities[i].forEach((value) => {
            if (value.spell.indexOf(this.keyword) > -1 || value.name.indexOf(this.keyword) > -1) {
              result.push(value)
            }
          })
        }
        this.list = result
      }, 100)
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.searchContent)
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.search
  background $bgColor
  height 0.73rem
  padding 0 0.1rem
  .search-input
    box-sizing border-box
    line-height 0.6rem
    height 0.6rem
    width 100%
    border-radius 0.04rem
    text-align center
    color #666
    padding 0 0.1rem
.search-content
  overflow hidden
  position absolute
  top 1.58rem
  left 0
  right 0
  bottom 0
  z-index 1
  background #eee
  .search-item
    line-height 0.62rem
    background #fff
    padding-left 0.2rem
    color #666
</style>
